# -*- coding: utf-8 -*-
"""
@Time ： 2024/1/11 0011 15:06
@Auth ： ysc
@File ：status_monitor_page.py
@IDE ：PyCharm
"""
import time
from Page.base_page import BasePage
from Page.login_page import LoginPage
from Lib.readelement import Element
from Lib.log import log

system_resource = Element('status_monitor')


class System_Resource_Page(BasePage):
    """系统资源页面"""

    def login(self, url=None, name=None, password=None):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.login(url, name, password)

    def check_status_monitor(self):
        """检查系统资源是否显示"""
        try:
            title = self.find_element(system_resource["name_title"])
            if title.is_displayed():
                title_info = 'admin用户登录，界面正常显示'
            else:
                title_info = '普通用户登录/用户未登录，界面不显示'
            return title_info
        except Exception as e:
            log.error("error: found status_monitor timeout")
            log.info(e)

    def resource_button_info(self):
        """
        页面信息检查--资源按钮
        """
        try:
            self.click(system_resource["resource_button"])
        except Exception as e:
            log.error("error: found resource_button timeout")
            log.info(e)

    def service_button_info(self):
        """
        页面信息检查——服务按钮
        """
        try:
            self.click(system_resource["service_button"])
        except Exception as e:
            log.error("error: found service_button timeout")
            log.info(e)

    def home_button_info(self):
        """
        页面信息检查——首页按钮
        """
        try:
            self.click(system_resource["home_button"])
        except Exception as e:
            log.error("error: found home_button timeout")
            log.info(e)

    def box_select_info(self):
        """页面信息检查——test_box下拉框"""
        try:
            select_test_box = self.find_element(system_resource["test_box"])
            if select_test_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found test_box timeout")
            log.info(e)

    def os_box_select_info(self):
        """页面信息检查——os_box下拉框"""
        try:
            select_os_box = self.find_element(system_resource["os_box"])
            if select_os_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found os_box timeout")
            log.info(e)

    def arch_box_select_info(self):
        """页面信息检查——arch_box下拉框"""
        try:
            select_arch_box = self.find_element(system_resource["arch_box"])
            if select_arch_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found arch_box timeout")
            log.info(e)

    def job_stage_box_select_info(self):
        """页面信息检查——job_stage"""
        try:
            select_job_stage_box = self.find_element(system_resource["job_stage"])
            if select_job_stage_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found job_stage timeout")
            log.info(e)

    def job_health_box_select_info(self):
        """页面信息检查——job_health"""
        try:
            select_job_stage_box = self.find_element(system_resource["job_health"])
            if select_job_stage_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found job_health timeout")
            log.info(e)

    def search_button(self):
        """页面信息——搜索按钮"""
        try:
            self.click(system_resource["search_button"])
        except Exception as e:
            log.error("error: found search_button timeout")
            log.info(e)

    def reset_button(self):
        """页面信息——重置按钮"""
        try:
            self.click(system_resource["reset_button"])
        except Exception as e:
            log.error("error: found reset_button timeout")
            log.info(e)

    def get_job_count(self):
        """获取job数量"""
        try:
            count = self.get_element_text(system_resource["jobs_count"])
            return int(count[1:-2])
        except Exception as e:
            log.error("error: found jobs_count timeout")
            log.info(e)

    #################################################zpt_test

    def job_stage_box(self):
        """获取job数量"""
        try:
            self.click(system_resource["job_stage_box"])
        except Exception as e:
            log.error("error: found search_button timeout")
            log.info(e)

    def check_job_list_titlie(self):
        job_list_flag = self.check_elements(['job_list_id', 'job_list_suite', 'job_list_arch', 'job_list_stage',
                                             'job_list_health', 'submit_time', 'start_time', 'job_list_duration',
                                             'job_list_testbox', 'job_list_os', 'job_list_account'])
        return job_list_flag

    def check_start_time(self):
        text = self.get_element_text(system_resource['start_time'])
        return text

    def set_user_search_text(self, name):
        """
        project搜索框输入搜索内容
        """
        self.send_keys(system_resource['user_input'], name)

    def set_job_id_search_text(self, name):
        """
        job_id搜索框输入搜索内容
        """
        self.send_keys(system_resource['job_id_autocomplete'], name)

    def click_search_button(self):
        """
        点击搜索按钮
        """
        self.click(system_resource['search_button'])

    def get_suite_text(self):
        """
        获取测试套文本
        """
        text = self.get_element_text(system_resource['rpm_build'])
        return text

    def get_job_id(self):
        """
        获取测试套文本
        """
        text = self.get_element_text(system_resource['first_job_id'])
        return text

    def check_elements(self, eles_list):
        """
         页面信息查询
        """
        try:
            flag = True
            for ele in eles_list:
                log.info('check element ' + ele)
                flag = self.is_element_exist(system_resource[ele])
                if not flag:
                    break
            return flag
        except Exception as e:
            log.error('check element ' + ele + ' failed' + 'flag:' + str(flag), e)

    def click_reset_button(self):
        """
        点击搜索按钮
        """
        self.click(system_resource['reset'])

    def click_job_id(self):
        """
        点击搜索按钮
        """
        self.click(system_resource['first_job_id'])


